<?php


namespace app\api\controller\v1;


use app\exception\ThemeException;
use app\validate\api\IDCollection;
use app\api\model\Theme as ThemeModel;
use app\validate\api\IDMustBePositiveInt;
use think\annotation\Route;
use think\Collection;
use think\Exception;

class Theme
{
    /**
     * @Route("v1/theme",method="GET",https=0)
     *  ?ids=id1,id2,id3
     *
     * @param string $ids
     *
     * @return \think\response\Json
     * @throws Exception
     */
    public function getSimpleList($ids=''): \think\response\Json
    {
        (new IDCollection())->goCheck();
        $ids = explode(',',$ids);
        $result = ThemeModel::with(['topicImg','headImg'])->select($ids);
        if((count($result)-count($ids))!=0){
           throw new ThemeException();
        }
        return json($result);
    }

    /**
     * @param $id
     * @Route("v1/theme/:id",method="GET",https=0)
     *
     * @return mixed
     * @throws Exception
     */
    public function getComplexOne($id){
        (new IDMustBePositiveInt())->goCheck();
        $result = ThemeModel::getThemeWithProducts($id);
        if($result->isEmpty()){
            throw new ThemeException();
        }
        return $result;
    }
}